package com.gyy.faq.service;

import com.gyy.faq.controller.Result;
import com.gyy.faq.dao.FaqBackFeedDAO;
import com.gyy.faq.dao.FaqClassifyDAO;
import com.gyy.faq.dao.FaqCommitDAO;
import com.gyy.faq.dao.FaqProblemDAO;
import com.gyy.faq.pojo.dto.FaqBackFeedDTO;
import com.gyy.faq.pojo.dto.FaqClassifyDTO;
import com.gyy.faq.pojo.dto.FaqProblemDTO;
import com.gyy.faq.pojo.po.FaqBackFeedPO;
import com.gyy.faq.pojo.po.FaqClassifyPO;
import com.gyy.faq.pojo.po.FaqCommitPO;
import com.gyy.faq.pojo.po.FaqProblemPO;
import com.gyy.faq.pojo.vo.FaqClassifyVO;
import com.gyy.faq.pojo.vo.FaqProblemVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.*;

/**
 * Created by Lowi on 2018/6/25 0025.
 */
@Service
public class FaqService {
	@Autowired
	private FaqProblemDAO faqProblemDao;
	@Autowired
	private FaqClassifyDAO faqClassifyDao;
	@Autowired
	private FaqCommitDAO faqCommitDao;
	@Autowired
	private FaqBackFeedDAO faqBackFeedDao;
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	@Autowired
	private RedisTemplate redisTemplate;

	public Result selectHotProblem() {
		List<FaqProblemPO> faqProblems = faqProblemDao.selectHotProblem();
		List<FaqProblemVO> faqProblemVOS = new ArrayList<>();
		for (FaqProblemPO faqProblem : faqProblems) {
			FaqProblemVO faqProblemVO = new FaqProblemVO();
			faqProblemVO.setId(faqProblem.getId());
			faqProblemVO.setTitle(faqProblem.getTitle());
			faqProblemVOS.add(faqProblemVO);
		}
		Result result = new Result(faqProblems.size() > 0 ? 0 : 1, faqProblems.size() > 0 ? "查询成功" : "暂无数据");
		result.addMapData("list", faqProblemVOS);
		return result;
	}

	public Result selectProblemInfo(FaqProblemDTO faqProblemDTO) {
		faqProblemDao.updateProblemClickCount(faqProblemDTO.getId());

		FaqProblemPO faqProblemPO = faqProblemDao.selectByPrimaryKey(faqProblemDTO.getId());

		FaqProblemVO faqProblemVO = new FaqProblemVO();
		faqProblemVO.setTitle(faqProblemPO.getTitle());
		faqProblemVO.setComment(faqProblemPO.getComment());

		FaqClassifyPO classifyPO = faqClassifyDao.selectByPrimaryKey(faqProblemPO.getClassifyId());


		if (classifyPO.getRank().equals(1)) {
			faqProblemVO.setOneCategory(classifyPO.getClassifyName());
			faqProblemVO.setOneCategoryId(classifyPO.getId());
		}
		if (classifyPO.getRank().equals(2)) {
			FaqClassifyPO oneClassify = faqClassifyDao.selectByPrimaryKey(classifyPO.getParentId());
			faqProblemVO.setOneCategory(oneClassify.getClassifyName());
			faqProblemVO.setTwoCategory(classifyPO.getClassifyName());

			faqProblemVO.setOneCategoryId(oneClassify.getId());
			faqProblemVO.setTwoCategoryId(classifyPO.getId());

		}
		if (classifyPO.getRank().equals(3)) {
			FaqClassifyPO twoClassifyPO = faqClassifyDao.selectByPrimaryKey(classifyPO.getParentId());
			faqProblemVO.setTwoCategory(twoClassifyPO.getClassifyName());

			FaqClassifyPO oneClassify = faqClassifyDao.selectByPrimaryKey(twoClassifyPO.getParentId());
			faqProblemVO.setOneCategory(oneClassify.getClassifyName());

			faqProblemVO.setThreeCategory(classifyPO.getClassifyName());

			faqProblemVO.setOneCategoryId(oneClassify.getId());
			faqProblemVO.setTwoCategoryId(twoClassifyPO.getId());
			faqProblemVO.setThreeCategoryId(classifyPO.getId());

		}


		faqProblemVO.setCreateTime(faqProblemPO.getCreateTime());


		Result result = new Result(faqProblemPO != null ? 0 : 1, faqProblemPO != null ? "查询成功" : "暂无数据");
		result.addMapData("list", faqProblemVO);
		return result;
	}

	public Result homepageClassifyList() {
		List<FaqClassifyPO> faqClassifyPOS = faqClassifyDao.selectParentClassifyList();
		List<FaqClassifyVO> faqClassifyVOS = new ArrayList<>();
		for (FaqClassifyPO faqClassifyPO : faqClassifyPOS) {
			FaqClassifyVO faqClassifyVO = new FaqClassifyVO();
			faqClassifyVO.setId(faqClassifyPO.getId());
			faqClassifyVO.setClassifyName(faqClassifyPO.getClassifyName());
			List<FaqClassifyPO> underClassifyList = faqClassifyDao.getUnderClassifyList(faqClassifyPO.getId());
			faqClassifyVO.setTwoCategory(underClassifyList);
			faqClassifyVOS.add(faqClassifyVO);
		}
		Result result = new Result(faqClassifyVOS.size() > 0 ? 0 : 1, faqClassifyVOS.size() > 0 ? "查询成功" : "暂无数据");
		result.addMapData("list", faqClassifyVOS);
		return result;
	}

	public Result selectProblemList(FaqProblemDTO faqProblemDTO) {
		if (faqProblemDTO != null && faqProblemDTO.getClassifyId() != null) {

			FaqClassifyPO faqClassifyPO = faqClassifyDao.selectByPrimaryKey(faqProblemDTO.getClassifyId());
			if (faqClassifyPO.getRank().equals(1)) {
				faqProblemDTO.setClassifyParentId(faqProblemDTO.getClassifyId());
				faqProblemDTO.setClassifyId(null);
				List<FaqProblemPO> faqProblemPOS = faqProblemDao.selectProblemList(faqProblemDTO);
				int total = faqProblemDao.selectProblemTotal(faqProblemDTO);
				Result result = new Result(faqProblemPOS.size() > 0 ? 0 : 1, faqProblemPOS.size() > 0 ? "查询成功" : "暂无数据");
				result.addMapData("list", faqProblemPOS);
				result.addMapData("oneCategory", faqClassifyPO.getClassifyName());
				result.addMapData("oneCategoryId", faqClassifyPO.getId());
				result.addMapData("total", total);
				return result;
			}
			if (faqClassifyPO.getRank().equals(2)) {
				FaqClassifyPO classifyPO = faqClassifyDao.selectByPrimaryKey(faqClassifyPO.getParentId());

				faqProblemDTO.setHighLevelId(faqProblemDTO.getClassifyId());
				faqProblemDTO.setClassifyId(null);
				List<FaqProblemPO> faqProblemPOS = faqProblemDao.selectProblemList(faqProblemDTO);
				int total = faqProblemDao.selectProblemTotal(faqProblemDTO);
				Result result = new Result(faqProblemPOS.size() > 0 ? 0 : 1, faqProblemPOS.size() > 0 ? "查询成功" : "暂无数据");
				result.addMapData("list", faqProblemPOS);
				result.addMapData("oneCategory", classifyPO.getClassifyName());
				result.addMapData("oneCategoryId", classifyPO.getId());
				result.addMapData("twoCategory", faqClassifyPO.getClassifyName());
				result.addMapData("twoCategoryId", faqClassifyPO.getId());
				result.addMapData("total", total);
				return result;
			}
			if (faqClassifyPO.getRank().equals(3)) {
				FaqClassifyPO twoClassifyPO = faqClassifyDao.selectByPrimaryKey(faqClassifyPO.getParentId());
				FaqClassifyPO oneClassifyPO = faqClassifyDao.selectByPrimaryKey(twoClassifyPO.getParentId());

				List<FaqProblemPO> faqProblemPOS = faqProblemDao.selectProblemList(faqProblemDTO);
				int total = faqProblemDao.selectProblemTotal(faqProblemDTO);
				Result result = new Result(faqProblemPOS.size() > 0 ? 0 : 1, faqProblemPOS.size() > 0 ? "查询成功" : "暂无数据");
				result.addMapData("list", faqProblemPOS);
				result.addMapData("oneCategory", oneClassifyPO.getClassifyName());
				result.addMapData("oneCategoryId", oneClassifyPO.getId());
				result.addMapData("twoCategory", twoClassifyPO.getClassifyName());
				result.addMapData("twoCategoryId", twoClassifyPO.getId());
				result.addMapData("threeCategory", faqClassifyPO.getClassifyName());
				result.addMapData("threeCategoryId", faqClassifyPO.getId());
				result.addMapData("total", total);
				return result;
			}
		}
		List<FaqProblemPO> faqProblemPOS = faqProblemDao.selectProblemList(faqProblemDTO);
		int total = faqProblemDao.selectProblemTotal(faqProblemDTO);
		Result result = new Result(faqProblemPOS.size() > 0 ? 0 : 1, faqProblemPOS.size() > 0 ? "查询成功" : "暂无数据");
		result.addMapData("list", faqProblemPOS);
		result.addMapData("total", total);
		return result;
	}

	public Result insertParentClassify(FaqClassifyDTO faqClassifyDTO) {
		FaqClassifyPO faqClassifyPO = new FaqClassifyPO();
		faqClassifyPO.setClassifyName(faqClassifyDTO.getClassifyName());
		faqClassifyPO.setRank(1);
		faqClassifyPO.setCreateTime(new Date());
		int insertSelective = faqClassifyDao.insertSelective(faqClassifyPO);
		Result result = new Result(insertSelective > 0 ? 0 : 1, insertSelective > 0 ? "添加成功" : "添加失败");
		return result;
	}

	public Result insertSonClassify(FaqClassifyDTO faqClassifyDTO) {
		FaqClassifyPO classifyPO = faqClassifyDao.selectByPrimaryKey(faqClassifyDTO.getId());

		FaqClassifyPO faqClassifyPO = new FaqClassifyPO();
		faqClassifyPO.setClassifyName(faqClassifyDTO.getClassifyName());
		faqClassifyPO.setParentId(faqClassifyDTO.getId());
		faqClassifyPO.setRank(classifyPO.getRank() + 1);
		faqClassifyPO.setCreateTime(new Date());
		int insertSelective = faqClassifyDao.insertSelective(faqClassifyPO);
		Result result = new Result(insertSelective > 0 ? 0 : 1, insertSelective > 0 ? "添加成功" : "添加失败");
		return result;
	}

	public Result insertProblem(FaqProblemDTO faqProblemDTO) {
		FaqClassifyPO classifyPO = faqClassifyDao.selectByPrimaryKey(faqProblemDTO.getClassifyId());

		FaqProblemPO faqProblemPO = new FaqProblemPO();
		faqProblemPO.setCreateTime(new Date());
		faqProblemPO.setTitle(faqProblemDTO.getTitle());
		faqProblemPO.setComment(faqProblemDTO.getComment());
		faqProblemPO.setClassifyId(faqProblemDTO.getClassifyId());

		if (classifyPO.getRank().equals(1)) {
			faqProblemPO.setClassifyParentId(faqProblemDTO.getClassifyId());
		}
		if (classifyPO.getRank().equals(2)) {
			faqProblemPO.setClassifyParentId(classifyPO.getParentId());
			faqProblemPO.setHighLevelId(classifyPO.getParentId());
		}
		if (classifyPO.getRank().equals(3)) {
			faqProblemPO.setHighLevelId(classifyPO.getParentId());
			FaqClassifyPO faqClassifyPO = faqClassifyDao.selectByPrimaryKey(classifyPO.getParentId());
			faqProblemPO.setClassifyParentId(faqClassifyPO.getParentId());
		}
		int insertSelective = faqProblemDao.insertSelective(faqProblemPO);
		Result result = new Result(insertSelective > 0 ? 0 : 1, insertSelective > 0 ? "添加成功" : "添加失败");
		return result;
	}

	public Result editProblem(FaqProblemDTO faqProblemDTO) {
		FaqProblemPO faqProblemPO = new FaqProblemPO();
		faqProblemPO.setTitle(faqProblemDTO.getTitle());
		faqProblemPO.setComment(faqProblemDTO.getComment());
		faqProblemPO.setId(faqProblemDTO.getId());

		FaqClassifyPO classifyPO = faqClassifyDao.selectByPrimaryKey(faqProblemDTO.getClassifyId());
		if (classifyPO.getRank().equals(1)) {
			faqProblemPO.setClassifyParentId(faqProblemDTO.getClassifyId());
		}
		if (classifyPO.getRank().equals(2)) {
			faqProblemPO.setClassifyParentId(classifyPO.getParentId());
			faqProblemPO.setHighLevelId(classifyPO.getParentId());
		}
		if (classifyPO.getRank().equals(3)) {
			faqProblemPO.setHighLevelId(classifyPO.getParentId());
			FaqClassifyPO faqClassifyPO = faqClassifyDao.selectByPrimaryKey(classifyPO.getParentId());
			faqProblemPO.setClassifyParentId(faqClassifyPO.getParentId());
		}
		faqProblemPO.setClassifyId(faqProblemDTO.getClassifyId());
		faqProblemPO.setCreateTime(new Date());
		int updateByPrimaryKeySelective = faqProblemDao.updateByPrimaryKeySelective(faqProblemPO);

		FaqCommitPO faqCommitPO = new FaqCommitPO();
		faqCommitPO.setProblemId(faqProblemDTO.getId());
		faqCommitPO.setMsg(faqProblemDTO.getMsg());
		faqCommitPO.setCreateTime(new Date());
		faqCommitDao.insertSelective(faqCommitPO);

		Result result = new Result(updateByPrimaryKeySelective > 0 ? 0 : 1, updateByPrimaryKeySelective > 0 ? "修改成功" : "修改失败");
		return result;
	}

	public Result deleteProblem(FaqProblemDTO faqProblemDTO) {

		faqBackFeedDao.deleteInfoByProblemId(faqProblemDTO.getId());
		faqCommitDao.deleteInfoByProblemId(faqProblemDTO.getId());

		int deleteByPrimaryKey = faqProblemDao.deleteByPrimaryKey(faqProblemDTO.getId());
		Result result = new Result(deleteByPrimaryKey > 0 ? 0 : 1, deleteByPrimaryKey > 0 ? "删除成功" : "删除失败");
		return result;
	}

	public Result editProblemClassify(FaqProblemDTO faqProblemDTO) {
		boolean flag = true;
		FaqClassifyPO classifyPO = faqClassifyDao.selectByPrimaryKey(faqProblemDTO.getClassifyId());
		for (Integer id : faqProblemDTO.getIds()) {
			FaqProblemPO faqProblemPO = new FaqProblemPO();
			faqProblemPO.setClassifyId(faqProblemDTO.getClassifyId());
			faqProblemPO.setId(id);
			faqProblemPO.setCreateTime(new Date());

			if (classifyPO.getRank().equals(1)) {
				faqProblemPO.setClassifyParentId(faqProblemDTO.getClassifyId());
			}
			if (classifyPO.getRank().equals(2)) {
				faqProblemPO.setClassifyParentId(classifyPO.getParentId());
				faqProblemPO.setHighLevelId(classifyPO.getParentId());
			}
			if (classifyPO.getRank().equals(3)) {
				faqProblemPO.setHighLevelId(classifyPO.getParentId());
				FaqClassifyPO faqClassifyPO = faqClassifyDao.selectByPrimaryKey(classifyPO.getParentId());
				faqProblemPO.setClassifyParentId(faqClassifyPO.getParentId());
			}
			int updateByPrimaryKeySelective = faqProblemDao.updateByPrimaryKeySelective(faqProblemPO);
			if (updateByPrimaryKeySelective <= 0) {
				flag = false;
			}
		}
		Result result = new Result(flag ? 0 : 1, flag ? "修改成功" : "部分修改失败");
		return result;
	}

	public Result deleteClassify(FaqClassifyDTO faqClassifyDTO) {
		FaqProblemDTO faqProblemDTO = new FaqProblemDTO();
		faqProblemDTO.setClassifyId(faqClassifyDTO.getId());
		List<FaqProblemPO> faqProblemPOS = faqProblemDao.selectProblemList(faqProblemDTO);
		if (faqProblemPOS.size() > 0) {
			return new Result(1, "该分类拥有问题内容，删除失败");
		}

		FaqClassifyPO faqClassifyPO = faqClassifyDao.selectByPrimaryKey(faqClassifyDTO.getId());
		if (faqClassifyPO.getRank().equals(1) || faqClassifyPO.getRank().equals(2)) {
			List<FaqClassifyPO> underClassifyList = faqClassifyDao.getUnderClassifyList(faqClassifyPO.getId());
			if (underClassifyList.size() > 0) {
				return new Result(1, "该分类拥有子分类，删除失败");
			}
			int flag = faqClassifyDao.deleteByPrimaryKey(faqClassifyDTO.getId());
			Result result = new Result(flag > 0 ? 0 : 1, flag > 0 ? "删除成功" : "删除失败");
			return result;
		}
		if (faqClassifyPO.getRank().equals(3)) {
			int flag = faqClassifyDao.deleteByPrimaryKey(faqClassifyDTO.getId());
			Result result = new Result(flag > 0 ? 0 : 1, flag > 0 ? "删除成功" : "删除失败");
			return result;
		}
		return null;
	}

	public Result selectClassifyList(FaqClassifyDTO faqClassifyDTO) {

		List<FaqClassifyPO> faqClassifyPOS = faqClassifyDao.selectParentClassifyList();

		List<FaqClassifyVO> faqClassifyVOS = new ArrayList<>();

		for (FaqClassifyPO faqClassifyPO : faqClassifyPOS) {
			FaqClassifyVO faqClassifyVO = new FaqClassifyVO();
			faqClassifyVO.setId(faqClassifyPO.getId());
			faqClassifyVO.setClassifyName(faqClassifyPO.getClassifyName());
			List<FaqClassifyPO> underClassifyList = faqClassifyDao.getUnderClassifyList(faqClassifyPO.getId());

			if (underClassifyList.size() > 0) {
				faqClassifyVO.setFlag(true);
			}

			List<FaqClassifyVO> faqClassifyVOS2 = new ArrayList<>();
			for (FaqClassifyPO classifyPO : underClassifyList) {
				FaqClassifyVO faqClassifyVO2 = new FaqClassifyVO();
				faqClassifyVO2.setId(classifyPO.getId());
				faqClassifyVO2.setClassifyName(classifyPO.getClassifyName());
				List<FaqClassifyPO> classifyList = faqClassifyDao.getUnderClassifyList(classifyPO.getId());
				if (classifyList.size() > 0) {
					faqClassifyVO2.setFlag(true);
				}
				faqClassifyVOS2.add(faqClassifyVO2);
				faqClassifyVO.setCategory(faqClassifyVOS2);
				List<FaqClassifyVO> faqClassifyVOS3 = new ArrayList<>();
				for (FaqClassifyPO classifyPO3 : classifyList) {
					FaqClassifyVO faqClassifyVO3 = new FaqClassifyVO();
					faqClassifyVO3.setId(classifyPO3.getId());
					faqClassifyVO3.setClassifyName(classifyPO3.getClassifyName());
					faqClassifyVO3.setFlag(false);
					faqClassifyVOS3.add(faqClassifyVO3);
					faqClassifyVO2.setCategory(faqClassifyVOS3);
				}
			}
			faqClassifyVOS.add(faqClassifyVO);
		}
		Result result = new Result(faqClassifyVOS.size() > 0 ? 0 : 1, faqClassifyVOS.size() > 0 ? "查询成功" : "暂无数据");
		result.addMapData("list", faqClassifyVOS);
		return result;
	}

	public Result submitBackFeed(FaqBackFeedDTO faqBackFeedDTO) {
		FaqBackFeedPO faqBackFeedPO = new FaqBackFeedPO();
		faqBackFeedPO.setCreateTime(new Date());
		faqBackFeedPO.setComment(faqBackFeedDTO.getComment());
		faqBackFeedPO.setProblemId(faqBackFeedDTO.getProblemId());
		faqBackFeedPO.setStatus(0);
		int flag = faqBackFeedDao.insertSelective(faqBackFeedPO);

		Result result = new Result(flag > 0 ? 0 : 1, flag > 0 ? "提交成功" : "提交失败");
		return result;
	}

	public Result updateBackFeed(FaqBackFeedDTO faqBackFeedDTO) {
		FaqBackFeedPO faqBackFeedPO = faqBackFeedDao.selectByPrimaryKey(faqBackFeedDTO.getId());
		if (faqBackFeedPO.getStatus().equals(3)) {
			return new Result(1, "已接受问题");
		}
		FaqBackFeedPO backFeedPO = new FaqBackFeedPO();
		backFeedPO.setId(faqBackFeedDTO.getId());
		backFeedPO.setStatus(faqBackFeedDTO.getStatus());
		int flag = faqBackFeedDao.updateByPrimaryKeySelective(backFeedPO);
		Result result = new Result(flag > 0 ? 0 : 1, flag > 0 ? "提交成功" : "提交失败");
		return result;
	}

	public void redisTest() {
		stringRedisTemplate.opsForValue().set("key2", "赶快来啊");
		redisTemplate.delete("key2");
		System.out.println(stringRedisTemplate.opsForValue().get("key2"));

		ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
	}
}
